<?php 
include ('layout/header.php'); 
include ('common/paging.php'); 
?>

<?php
    $DALAcc = new DALUserAccount();
    $user = $DALAcc->getUser();
    $balance = $user['balance'];
    $status = $user['active'];
?>
<?php
    $page = Utils::getValGet('pn');
    $page = (int) $page;
    $page = ($page == 0) ? 1 : $page;

    $paging = new paging($page, "main.php");
    $itemPerPage = $paging->rowLimit;
    //load settledContract
    $dal = new DALSettledContract();
    $data = $dal->getListSettled('modify_timestamp', 'desc', $page, $itemPerPage);

    $totalRow = $dal->count();
    $paging->totalCount = $totalRow;
    $paging->doPaging();      


?>
<div class='info' style="overflow: hidden">
    <h3><span class='balance' style='float:left;'>Account : <?= $balance ?></span></h3>
    <button style="float:right;" id="btnManage" type="button" class="btn btn-<?= ($status == '1') ? 'danger' : 'primary' ?>"><?= ($status == '1') ? 'Stop' : 'Start' ?></button>
</div>

        <?php Utils::print_paging($paging); ?>
    
    <div class="checkbox">
    <label>
      <input type="checkbox" name="cbBetSlip" id="cbBetSlip"/> Hiện kèo
    </label>
  </div>
        <div class="table-responsive">
            <table class="table table-striped table-bordered table-condensed">
                <thead>
                    <tr>
                        <th>Giải</th>
                        <th>Chủ</th>
                        <th>Khách</th>
                        <th>Phút</th>
                        <th>Số Trái</th>
                        <th>Under</th>
                        <th>Số tiền</th>
                        <th>Tỉ lệ</th>
                        <th>Status</th>
                        <td class="bet_slip">[Bắt]<br/>Trái</td>
                        <td class="bet_slip">[Bắt]<br/>Phút</td>
                        <td class="bet_slip">[Bắt]<br/>Ăn</td>
                        <th class="bet_slip">Updated on</th>
                        <th class="bet_slip">Kèo</th>
                    </tr>
                </thead>
                <tbody>       
                    <?php $runningCount = 0; ?>
                    <?php foreach ($data as $row) : ?>
                        <?php 
                            $rowCss = ''; 
                            $rowStatus = '';
                        ?>
                        <?php
                            if($row->bet_slip == 'TEST_MODE'){
                                $rowCss = ' test_mode';
                            }
                            
                            if($row->running == '1'){
                                $rowStatus = ($row->dangerous == '1') ? 'rfire' : 'rball';
                                $runningCount++;
                            }else{
                                $rowStatus = ($row->won == '1') ? 'victory' : 'failed';
                                if($row->dangerous == '1'){
                                    $rowStatus .= ' fire';
                                }
                            }
                            
                        ?>
                        <tr class="<?=$rowCss?>">                                
                            <input type="hidden" name="id_<?=$row->id?>"/>
                            <input type="hidden" name="contractid_<?=$row->contract_id?>"/>
                            <td><?= $row->country_name ?><br/><?= $row->league_name ?></td>
                            <td><?= $row->home_name ?></td>
                            <td><?= $row->away_name ?></td>
                            <td><?= $row->current_time ?></td>
                            <td><?= $row->current_goal ?></td>
                            <td><?= $row->under_goal ?></td>
                            <td><?= $row->bet_amount ?></td>
                            <td><?= $row->rate?></td>
                            <td class="status <?=$rowStatus ?>"><?= $row->won ?></td>
                            <td class="bet_slip"><?= $row->settled_total_goals ?></td>
                            <td class="bet_slip"><?= $row->settled_play_time ?></td>
                            <td class="bet_slip"><?= $row->won_amount ?></td>
                            <td class="bet_slip"><?= $row->modify_timestamp ?></td>
                            <td class="bet_slip"><?= $row->bet_slip ?></td>
                        </tr>
                    <?php endforeach; ?>
                        
                </tbody>
            </table>
            <?php Utils::print_paging($paging); ?>
        </div>
    <script>
        $(document).ready(function(){
            document.title = '<?= "($runningCount) :: $balance" ?> ' + document.title;
        })
                    
        $('#btnManage').click(function(){
            var status = ( $(this).text() === 'Stop' ) ? false : true;
            if(status){ //start app
                updateStatus(true);
            }else{ //stop app
                updateStatus(false);
            }
        });
        
        function updateStatus(val){
            $.ajax({
                type: 'POST',
                url: 'import.php',
                cache: false,
                data: {
                    validate: 'JYlOjo821t6fvSOvOM7H764mEV4BSNZt3XiqH2skDU'
                    ,type: 'appStatus'
                    ,id: <?= $_SESSION['id']; ?>
                    ,status: val
                },success:function(data){
                    if(val){
                        $('#btnManage').removeClass('btn-primary');                
                        $('#btnManage').addClass('btn-danger');
                        $('#btnManage').text('Stop');
                        alert("started");
                    }else{
                        if(!isNaN(data)){
                            $('#btnManage').removeClass('btn-danger');
                            $('#btnManage').addClass('btn-primary');
                            $('#btnManage').text('Start');
                            alert("Đã ngưng chơi tiếp. Còn " + data + " đang đợi kết quả");
                        }else{
                            alert('Error: ' + data);
                        }
                    }
                }
            });
        }
    </script>
<?php include ('layout/footer.php'); ?>